//
//  UIImageView+FadeRound.m
//  YiJuZhangShou2.0
//
//  Created by 冷悦 on 16/3/29.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "UIImageView+FadeRound.h"
#import "UIImageView+WebCache.h"
#import "NSString+Reform.h"

#ifndef kUIIMageViewFadeRoundBorderColor
#define kUIIMageViewFadeRoundBorderColor [UIColor clearColor]
#endif

#ifndef kUIIMageViewFadeRoundBorderWidth
#define kUIImageViewFadeRoundBorderWidth 0.0f
#endif

#ifndef kUIIMageViewFadeRoundBorderInset
#define kUIImageViewFadeRoundBorderInset 0.0f
#endif

#ifndef kUIImageViewFadeRoundBorderFadeTime
#define kUIImageViewFadeRoundBorderFadeTime 0.5f
#endif

#ifndef kUIImageViewRadius
#define kUIImageViewRadius 0.0f
#endif

#ifndef kUIImageViewFadeRoundBorderAvatar
#define kUIImageViewFadeRoundBorderAvatar @"avatar.jpg"
#endif

@implementation UIImageView (FadeRound)

#pragma mark - Public
- (void)setRoundImage:(NSString *)imageURL
     PlaceHolderImage:(NSString *)placeHolderName
          borderWidth:(CGFloat)borderWidth
          borderColor:(UIColor *)borderColor
            andRadius:(CGFloat)radius{
    __weak typeof(self) weakSelf = self;
    
    if (![imageURL containsString:@"http"]) {
        imageURL = [NSString pictureAbsolutePathStrWithRelativePathStr:imageURL];
    }
    [self sd_setImageWithURL:[NSURL URLWithString:imageURL]
            placeholderImage:[UIImage imageNamed:placeHolderName]
                     options:SDWebImageAvoidAutoSetImage
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                       if (image != nil) {
                           NSDictionary *params = @{@"borderColor":borderColor, @"borderWidth":@(borderWidth),@"radius":@(radius)};
                           UIImage *circleImage = [self circleImage:image withParam:params];
                           weakSelf.image = circleImage;
                           self.alpha = 0.0;
                           [UIView animateWithDuration:kUIImageViewFadeRoundBorderFadeTime animations:^{
                               self.alpha = 1.0;
                           }];
                       }
                       
                   }];
}

- (void)setRoundImage:(NSString *)imageURL{
    [self setRoundImage:imageURL
       PlaceHolderImage:kUIImageViewFadeRoundBorderAvatar
            borderWidth:kUIImageViewFadeRoundBorderWidth
            borderColor:kUIIMageViewFadeRoundBorderColor
              andRadius:kUIImageViewRadius];
}

- (void)setRoundImage:(NSString *)imageURL withPlaceHolder:(NSString *)placeHolder andRadius:(CGFloat)radius{
    [self setRoundImage:imageURL PlaceHolderImage:placeHolder borderWidth:kUIImageViewFadeRoundBorderWidth borderColor:kUIIMageViewFadeRoundBorderColor andRadius:radius];
}

- (UIImage *)circleImage:(UIImage *)image withParam:(NSDictionary *)params{
    UIColor *borderColor;
    CGFloat borderWidth = 0.0;
    CGFloat borderInset = 0.0;
    CGFloat radius = 0.0;
    if(params != nil) {
        UIColor *color = [params objectForKey:@"borderColor"];
        borderColor = color == nil ? kUIIMageViewFadeRoundBorderColor : color;
        NSNumber *width = [params objectForKey:@"borderWidth"];
        borderWidth = [width floatValue] == 0.0? 0.0f : [width floatValue];
        if (borderWidth == 0) {
            borderInset = 0;
        }else{
            borderInset = 5;
        }
        radius = [params objectForKey:@"radius"] == nil?0.0f:[[params objectForKey:@"radius"] floatValue];
    }else{
        borderColor = kUIIMageViewFadeRoundBorderColor;
        borderWidth = kUIImageViewFadeRoundBorderWidth;
        borderInset = kUIImageViewFadeRoundBorderInset;
    }
    
    UIGraphicsBeginImageContext(radius == 0?image.size:CGSizeMake(radius, radius));
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
    CGRect rect;
    CGRect bigRect;
    CGFloat offset = borderInset-borderWidth < 0?0:borderInset-borderWidth;
    if (radius == 0) {
            rect = CGRectMake(borderInset, borderInset, image.size.width - borderInset*2, image.size.height - borderInset*2);
            bigRect = CGRectMake(offset, offset, image.size.width - offset*2, image.size.height - offset*2);
    }else{
        rect = CGRectMake(borderInset, borderInset, radius - borderInset*2, radius - borderInset*2);
        bigRect = CGRectMake(offset, offset, radius - offset*2, radius - offset*2);
    }
    
    [borderColor set];
    CGContextAddEllipseInRect(context, bigRect);
    CGContextFillPath(context);
    
    CGContextAddEllipseInRect(context, rect);
    CGContextClip(context);
    
    
    //在圆区域内画出image原图
    [image drawInRect:rect];
    CGContextAddEllipseInRect(context, rect);
    CGContextStrokePath(context);
    //生成新的image
    UIImage *circleImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return circleImage;
}

@end
